import {audit, queryById, traces} from 'services/order'
import {message} from 'antd'

export default {
  namespace: 'order',

  state: {
    data: {},
  },

  effects: {
    * query ({payload = {}}, {call, put}) {
      const {code, msg, data} = yield call(queryById, payload)
      if (code === 0) {
        yield put({type: 'updateState', payload: {data}})
      } else {
        throw {code, message: msg}
      }
    },

    * audit ({payload = {}}, {call, put}) {
      const {code, msg} = yield call(audit, payload)
      if (code === 0) {
        message.success(msg)
      } else {
        throw {code, message: msg}
      }
    },

    * traces ({payload = {}}, {call, put}) {
      const {code, msg} = yield call(traces, payload)
      if (code === 0) {
        message.success(msg)
      } else {
        throw {code, message: msg}
      }
    },
  },

  reducers: {
    updateState (state, {payload}) {
      return {
        ...state,
        ...payload,
      }
    },
  },
}
